﻿/*
* makabaka.cpp
* 翻译器界面
* 版权所有(c)，马卡巴卡语语言研究组，保留所有权利
*/

#include "ChineseToPinyin.h"
#include "PinyinToMakabaka.h"
#include "UI.h"


#define LVER (wstring)_T(__DATE__) + L" " + _T(__TIME__)

struct character
{
	wstring hanzi;
	wstring selectedpinyin;
	bool ismulti = false;
};
vector<character> chinese;

enum Type
{
	Chinese,
	EnglishOrNumbers,
	Others
};

vector<Type> type;

int main()
{

	wcin.imbue(locale("chs"));
	wcout.imbue(locale("chs"));
	
	SetConsoleTitle(((wstring)L"Makabaka Translator [版本 " + LVER + L"]").c_str());
	SetOutputColor(7);
	wcout << L"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
	wcout << L"* 马卡巴卡语翻译器                                        *" << endl;
	wcout << L"* 版权所有(c)，马卡巴卡语语言研究组，保留所有权利         *" << endl;
	wcout << L"* 更多信息，请访问：https://gitee.com/apple-ba/makabaka   *" << endl;
	wcout << L"* 版本 " << (wstring)LVER << L"                               * " << endl;
	wcout << L"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
	wcout << endl;
	SetOutputColor();

	while (true)
	{

		//读入
		wcout << L"请输入中文/英文，按两次回车以结束输入。" << endl;
		wstring x;
		wstring a;
		while (true)
		{
			SetOutputColor(3);
			wcout << L"> ";
			SetOutputColor(7);
			getline(wcin, x);
			SetOutputColor();
			if (x == L"")
			{
				if (a.size() > 0)
					a.erase(a.size() - 1, 1);
				break;;
			}
			a += x;
			a += L"\n";
		}
		chinese.resize(a.size());
		type.resize(a.size());

		//字符分类与获取拼音
		for (int i = 0; i < a.size(); i++)
		{
			if (isHanziChar(a[i]))
			{
				type[i] = Chinese;
				chinese[i].hanzi = a[i];
				vector<wstring> pinyin;
				getAllPingyinForWord(a.substr(i, 1), pinyin);
				if (pinyin.size() > 1)
				{
					chinese[i].ismulti = true;
					for (int x = 0; x < g_length_multiToneWordTable; x++)
					{
						if (multiToneWordTable[x].hanzi == chinese[i].hanzi)
						{
							chinese[i].selectedpinyin = multiToneWordTable[x].pinyi;
							break;
						}
					}

				}
				else
				{
					chinese[i].selectedpinyin = pinyin[0];
					chinese[i].ismulti = false;
				}
			}
			else if (a[i] >= L'A' && a[i] <= L'Z' || a[i] >= L'a' && a[i] <= L'z' || a[i] >= L'0' && a[i] <= L'9')
			{
				type[i] = EnglishOrNumbers;
			}
			else type[i] = Others;
		}


	print:
		//打印结构
		bool multitone = false;
		for (int i = 0; i < a.size(); i++)
		{
			if (type[i] == Chinese)
			{
				if (chinese[i].ismulti == true)
				{
					SetOutputColor(6);
					wcout << chinese[i].hanzi << L' ';
					wcout << chinese[i].selectedpinyin;
					wcout << "(" << i + 1 << ")";
					SetOutputColor();
					multitone = true;
				}
				else
				{
					SetOutputColor(10);
					wcout << chinese[i].hanzi << L' ';
					wcout << chinese[i].selectedpinyin;
					SetOutputColor();
				}
				wcout << " ";
			}
			else
			{
				SetOutputColor(10);
				wcout << a.substr(i, 1);
				SetOutputColor();

			}
		}
		wcout << endl;

		//处理多音字
		if (multitone)
		{
			wcout << L"修改多音字的读音。" << endl;
			while (true)
			{
				wcout << L"请输入汉字后的序号，按0退出。" << endl;
				SetOutputColor(3);
				wcout << L"> ";
				SetOutputColor(7);
				int i;
				SetOutputColor(7);
				wcin >> i;
				SetOutputColor();
				if (i == 0)break;
				if (i > type.size())
				{
					SetOutputColor(4);
					wcout << L"非法输入！" << endl;
					SetOutputColor();
					continue;
				}
				else
				{
					i--;
					if (type[i] != Chinese || chinese[i].ismulti == false)
					{
						SetOutputColor(4);
						wcout << L"非法输入！" << endl;
						SetOutputColor();
						continue;
					}
					vector<wstring> pinyin;
					getAllPingyinForWord(chinese[i].hanzi, pinyin);
					wcout << L"选择汉字“" << chinese[i].hanzi << L"”的拼音。" << endl;
					for (int x = 0; x < pinyin.size(); x++)
					{
						wcout << x + 1 << L". " << pinyin[x] << endl;
					}
					wcout << pinyin.size() + 1 << L". 手动输入" << endl;
				SelectPinyin:
					wcout << L"请输入选项 [1 ~ " << pinyin.size() + 1 << L"]" << endl;
					SetOutputColor(3);
					wcout << L"> ";
					SetOutputColor(7);
					int q;
					wcin >> q;
					q--;
					if (q < pinyin.size())
					{
						chinese[i].selectedpinyin = pinyin[q];
					}
					else if(q==pinyin.size())
					{
						wcout << L"手动输入汉字“" << chinese[i].hanzi << L"”的读音。请务必输入合法拼音，否则可能造成意料之外的错误！" << endl;
						SetOutputColor(3);
						wcout << L"> ";
						SetOutputColor(7);

						wstring py;
						wcin >> py;
						chinese[i].selectedpinyin = py;
					}
					else
					{
						SetOutputColor(4);
						wcout << L"非法输入！" << endl;
						SetOutputColor();
						goto SelectPinyin;
					}
					goto print;
				}
			}
		}


		if (a.size() != 0)wcout << L"翻译结果如下：" << endl;
		else continue;
		SetOutputColor(14);
		bool lastCorE = false;
		for (unsigned long long i = 0; i < a.size(); i++)
		{
			if (type[i] == Chinese)
			{
				if (lastCorE)wcout << " ";

				if (type.size() >= i + 4)
				{
					if (type[i + 1] == Chinese && type[i + 2] == Chinese && type[i + 3] == Chinese && chinese[i].hanzi == L"马" && chinese[i + 1].hanzi == L"卡" && chinese[i + 2].hanzi == L"巴" && chinese[i + 3].hanzi == L"卡")
					{
						wcout << "ma ka ba ka";
						i += 3;
						lastCorE = true;
						continue;
					}
				}
				if (chinese[i].hanzi == L"的" && chinese[i].selectedpinyin == L"de")
					wcout << "de";
				else
					wcout << PinyinToMakabaka(chinese[i].selectedpinyin);
				lastCorE = true;
			}
			else if (type[i] == EnglishOrNumbers)
			{
				if (lastCorE)wcout << " ";
				wcout << EnglishToMakabaka(a.substr(i, 1));
				lastCorE = true;
			}
			else
			{
				wcout << a[i];
				lastCorE = false;
			}
		}
		SetOutputColor();
		wcout << endl;
	}
}